﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Koonsoft.HYCRM.DAL;
using Koonsoft.HYCRM.Model;
using log4net;

namespace Koonsoft.HYCRM.BLL
{
    public class TasksBLL
    {
        private ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private TasksDAL _dal = null;

        protected TasksDAL DAL
        {
            get
            {
                if (_dal == null)
                {
                    _dal = new TasksDAL();
                }

                return _dal;
            }
        }

        /// <summary>
        /// 在数据库中新增待办任务
        /// </summary>
        /// <param name="companySerialNumber"></param>
        /// <returns></returns>
        public bool AddNewTask(string companySerialNumber)
        {
            return DAL.AddNewTask(companySerialNumber);
        }

        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="companySerialNumber"></param>
        /// <returns></returns>
        public bool RemoveTask(string companySerialNumber)
        {
            return DAL.RemoveTask(companySerialNumber);
        }


        /// <summary>
        /// 根据任务编号获取任务详情
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public TaskInfo GetTaskByTaskID(int taskID)
        {
            return DAL.GetTaskByTaskID(taskID);
        }

        /// <summary>
        /// 根据分行主管ID获取分行待办任务
        /// </summary>
        /// <param name="empolyeeID"></param>
        /// <param name="auditMonth"></param>>
        /// <returns></returns>
        public DataTable GetBranchTasks(int empolyeeID, int auditMonth)
        {
            DataTable branchTasks = new DataTable();

            try
            {
                DataTable dt = DAL.GetBranchTasks(empolyeeID, auditMonth);

                branchTasks = dt.Copy();
                branchTasks.Rows.Clear();

                foreach (DataRow dr in dt.Rows)
                {
                    DateTime auditDate =
                        Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + dr["auditmonth"] + "-" + dr["auditday"]);

                    if (auditDate < DateTime.Now.AddDays(100) && auditDate >= DateTime.Now)
                    {
                        branchTasks.ImportRow(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("获取分行待办任务异常", ex);
            }

            return branchTasks;
        }

        /// <summary>
        /// 根据员工编号获取3个月的待办任务
        /// </summary>
        /// <param name="branchID"></param>
        /// <param name="empolyeeID"></param>
        /// <param name="auditMonth"></param>
        /// <param name="auditYear"></param>
        /// <returns></returns>
        public DataTable GetEmpolyeeTasks(int branchID, int empolyeeID, int auditMonth, int auditYear)
        {
            var dt = DAL.GetEmpolyeeTasks(branchID,empolyeeID,auditMonth,auditYear);

            var month1 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(1).Month;
            var year1 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(1).Year;

            dt.Merge(DAL.GetEmpolyeeTasks(branchID, empolyeeID, month1, year1));


            var month2 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(2).Month;
            var year2 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(2).Year;

            dt.Merge(DAL.GetEmpolyeeTasks(branchID, empolyeeID, month2, year2));

            return dt;
        }

        /// <summary>
        /// 根据分行编号获取3个月的待办任务
        /// </summary>
        /// <param name="branchID"></param>
        /// <param name="auditMonth"></param>
        /// <param name="auditYear"></param>
        /// <returns></returns>
        public DataTable GetBranchTasks(int branchID, int auditMonth, int auditYear)
        {
            var dt = DAL.GetBranchTasks(branchID, auditMonth, auditYear);

            var month1 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(1).Month;
            var year1 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(1).Year;

            dt.Merge(DAL.GetBranchTasks(branchID, month1, year1));


            var month2 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(2).Month;
            var year2 = DateTime.Parse(auditYear + "-" + auditMonth + "-01").AddMonths(2).Year;

            dt.Merge(DAL.GetBranchTasks(branchID, month2, year2));

            return dt;
        }

        /// <summary>
        /// 根据分行编号获取3个月的待办任务
        /// </summary>
        /// <param name="branchID"></param>
        /// <param name="auditMonth"></param>
        /// <param name="auditYear"></param>
        /// <returns></returns>
        public DataTable GetBranchTasksByMonth(int branchID, int auditMonth, int auditYear)
        {
            return DAL.GetBranchTasks(branchID, auditMonth, auditYear);
        }
    }
}
